package com.hxzy.service.impl;

import com.hxzy.dto.back.TableNameSaveDTO;
import com.hxzy.entity.TplAutocolumn;
import com.hxzy.mapper.TplAutocolumnMapper;
import com.hxzy.service.TplAutocolumnService;
import com.hxzy.vo.back.TableNameColumnsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/4/8 14:52
 * @Version 1.0
 */
@Service
public class TplAutocolumnServiceImpl extends ServiceImpl<TplAutocolumn,Long> implements TplAutocolumnService {

    private TplAutocolumnMapper  tplAutocolumnMapper;

    @Autowired
    public void setTplAutocolumnMapper(TplAutocolumnMapper tplAutocolumnMapper) {
        this.tplAutocolumnMapper = tplAutocolumnMapper;
        super.setMyBatisBaseDao(tplAutocolumnMapper);
    }


    @Override
    public List<TplAutocolumn> findByTableName(String tableName) {
        return this.tplAutocolumnMapper.findByTableName(tableName);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean save(TableNameSaveDTO saveDTO) {
        this.tplAutocolumnMapper.deleteByTableName(saveDTO.getTableName());

        //保存
        for(TableNameColumnsVO  vo: saveDTO.getColumns()){
           TplAutocolumn  tpl=new TplAutocolumn();

           BeanUtils.copyProperties(vo,tpl);

            tpl.setTableName(saveDTO.getTableName());
            //保存
            this.tplAutocolumnMapper.insert(tpl);
        }

        return true;
    }

    @Override
    public TplAutocolumn searchTablePrimaryKeyAndJavaType(String tableName) {
        return this.tplAutocolumnMapper.searchTablePrimaryKeyAndJavaType(tableName);
    }
}
